Source for file TinyGoogleSpell.class.php

Documentation is available at TinyGoogleSpell.class.php

  1. <?php
  2. /* *
  3.  * Tiny Spelling Interface for TinyMCE Spell Checking.
  4.  *
  5.  * Copyright © 2006 Moxiecode Systems AB
  6.  */
  7.  
  8. class TinyGoogleSpell {
  9.     var $lang;
  10.  
  11.     function TinyGoogleSpell(&$config$lang$mode$spelling$jargon$encoding{
  12.         $this->lang = $lang;
  13.     }
  14.  
  15.     // Returns array with bad words or false if failed.
  16.     function checkWords($word_array{
  17.         $words array();
  18.         $wordstr implode(' '$word_array);
  19.  
  20.         $matches $this->_getMatches($wordstr);
  21.  
  22.         for ($i=0$i<count($matches)$i++)
  23.             $words[$this->unhtmlentities(mb_substr($wordstr$matches[$i][1]$matches[$i][2]"UTF-8"));
  24.  
  25.         return $words;
  26.     }
  27.  
  28.     function unhtmlentities($string{
  29.         $string preg_replace('~&#x([0-9a-f]+);~ei''chr(hexdec("\\1"))'$string);
  30.         $string preg_replace('~&#([0-9]+);~e''chr(\\1)'$string);
  31.  
  32.         $trans_tbl get_html_translation_table(HTML_ENTITIES);
  33.         $trans_tbl array_flip($trans_tbl);
  34.  
  35.         return strtr($string$trans_tbl);
  36.     }
  37.  
  38.     // Returns array with suggestions or false if failed.
  39.     function getSuggestion($word{
  40.         $sug array();
  41.  
  42.         $matches $this->_getMatches($word);
  43.  
  44.         if (count($matches0)
  45.             $sug explode("\t"utf8_encode($this->unhtmlentities($matches[0][4])));
  46.  
  47.         return $sug;
  48.     }
  49.  
  50.     function _xmlChars($string{
  51.        $trans get_html_translation_table(HTML_ENTITIESENT_QUOTES);
  52.     
  53.        foreach ($trans as $k => $v)
  54.             $trans[$k"&#".ord($k).";";
  55.  
  56.        return strtr($string$trans);
  57.     }
  58.  
  59.     function _getMatches($word_list{
  60.         $server "www.google.com";
  61.         $port 443;
  62.         $path "/tbproxy/spell?lang=" $this->lang . "&hl=en";
  63.         $host "www.google.com";
  64.         $url "https://" $server;
  65.  
  66.         // Setup XML request
  67.         $xml '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' $word_list '</text></spellrequest>';
  68.  
  69.         $header  "POST ".$path." HTTP/1.0 \r\n";
  70.         $header .= "MIME-Version: 1.0 \r\n";
  71.         $header .= "Content-type: application/PTI26 \r\n";
  72.         $header .= "Content-length: ".strlen($xml)." \r\n";
  73.         $header .= "Content-transfer-encoding: text \r\n";
  74.         $header .= "Request-number: 1 \r\n";
  75.         $header .= "Document-type: Request \r\n";
  76.         $header .= "Interface-Version: Test 1.4 \r\n";
  77.         $header .= "Connection: close \r\n\r\n";
  78.         $header .= $xml;
  79.         //$this->_debugData($xml);
  80.  
  81.         // Use raw sockets
  82.         $fp fsockopen("ssl://" $server$port$errno$errstr30);
  83.         if ($fp{
  84.             // Send request
  85.             fwrite($fp$header);
  86.  
  87.             // Read response
  88.             $xml "";
  89.             while (!feof($fp))
  90.                 $xml .= fgets($fp128);
  91.  
  92.             fclose($fp);
  93.         else {
  94.             // Use curl
  95.             $ch curl_init();
  96.             curl_setopt($chCURLOPT_URL,$url);
  97.             curl_setopt($chCURLOPT_RETURNTRANSFER1);
  98.             curl_setopt($chCURLOPT_CUSTOMREQUEST$header);
  99.             curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
  100.             $xml curl_exec($ch);
  101.             curl_close($ch);
  102.         }
  103.  
  104.         //$this->_debugData($xml);
  105.  
  106.         // Grab and parse content
  107.         preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/'$xml$matchesPREG_SET_ORDER);
  108.  
  109.         return $matches;
  110.     }
  111.  
  112.     function _debugData($data{
  113.         $fh @fopen("debug.log"'a+');
  114.         @fwrite($fh$data);
  115.         @fclose($fh);
  116.     }
  117. }
  118.  
  119. // Setup classname, should be the same as the name of the spellchecker class
  120. $spellCheckerConfig['class'"TinyGoogleSpell";
  121.  
  122. ?>

Documentation generated on Mon, 05 May 2008 16:23:17 +0400 by phpDocumentor 1.4.0